package com.atguigu.rabbitmq.springbootrabbitmq.config;

import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

@Configuration
public class TtlQueueConfig {
public static final String X_EXCHANGE="X";
public static final String Y_DEAD_LETTER_EXCHANGE="Y";
public static final String QUEUE_A="QA";
public static final String QUEUE_B="QB";
public static final String QUEUE_C="QC";
public static final String DEAD_LETTER_QUEUE="QD";

@Bean("xExchange")
    public DirectExchange xExchange(){
    return new DirectExchange(X_EXCHANGE);
}
@Bean("yExchange")
    public DirectExchange yExchange(){
    return new DirectExchange(Y_DEAD_LETTER_EXCHANGE);
}
@Bean("queueA")
    public Queue queueA(){
//    Map<String,Object> arguments=new HashMap<>();
//    arguments.put("x-dead-letter-exchange",Y_DEAD_LETTER_EXCHANGE);
//    arguments.put("x-dead-letter-routing-key","YD");
//    arguments.put("x-message-ttl",10000);
//    return QueueBuilder.durable(QUEUE_A).withArguments(arguments).build();
    return QueueBuilder.durable(QUEUE_A).deadLetterExchange(Y_DEAD_LETTER_EXCHANGE)
            .deadLetterRoutingKey("YD").ttl(10000)
            .build();
}
@Bean("queueB")
    public Queue queueB(){
    return QueueBuilder.durable(QUEUE_B).deadLetterExchange(Y_DEAD_LETTER_EXCHANGE)
            .deadLetterRoutingKey("YD").ttl(40000)
            .build();
}
@Bean("queueC")
    public Queue queueC(){
    return QueueBuilder.durable(QUEUE_C).deadLetterExchange(Y_DEAD_LETTER_EXCHANGE)
            .deadLetterRoutingKey("YD")
            .build();
}
@Bean("queueD")
    public Queue queueD(){
    return QueueBuilder.durable(DEAD_LETTER_QUEUE).build();
}
@Bean
    public Binding queueABindingX(@Qualifier("queueA") Queue queueA,@Qualifier("xExchange") DirectExchange xExchange){
    return BindingBuilder.bind(queueA).to(xExchange).with("XA");
}
@Bean
    public Binding queueBBindingX(@Qualifier("queueB") Queue queueB,@Qualifier("xExchange") DirectExchange xExchange){
    return BindingBuilder.bind(queueB).to(xExchange).with("XB");
}
@Bean
    public Binding queueCBindingX(){
    return BindingBuilder.bind(queueC()).to(xExchange()).with("XC");
}
//@Bean
//    public Binding queueDBindingY(@Qualifier("queueD") Queue queueD,@Qualifier("yExchange") DirectExchange yExchange){
//    return BindingBuilder.bind(queueD).to(yExchange).with("YD");
//}
 @Bean
    public Binding queueDBindingY(){
    return BindingBuilder.bind(queueD()).to(yExchange()).with("YD");
}
}
